from api.mod.eventGenerato import EventGenerator
from basic.entiys.charts.Authority import Authority
from basic.entiys.charts.BaseCharts import BaseChartsDTO, BaseCharts
from framework.adapter.auth import StringAuth2Instance
from framework.syntacticSugar.EventEntiys import EventEntiys

"""
高层协议：
在开发flask/django-restframe的时候 我们只需要调用这里的协议即可

"""


class Event:
    @classmethod
    def NoRoundEventExec(cls, event_name, event_entiys):
        """
        非回合执行的JSON (供API调用)
        :param event_entiys:
        :return:
        """
        try:
            event_generator = EventGenerator(event_name=event_name, event_entiys=event_entiys)
            return event_generator.render_json()
        except:
            return "错误的事件执行"

    @classmethod
    def ByAuthGetEvent(cls, charts, method_type, event_entiys:EventEntiys):
        """
        通过 角色的DTO 和 event_entiys 获取对应权能的事件列表
        :param charts: 角色 属于BaseCharts
        :param method_type: 权能的类型
        :return:
        """
        StringAuth2Instance.doit(charts)
        charts.fields.authority:Authority

        evnet_list = charts.fields.authority.AuthorityMapper.get(method_type) # 获取他的权能方案
        evnet_list = evnet_list if evnet_list is not None else []

        super_event_list = charts.fields.authority.special_event(event_entiys)
        super_event_list = super_event_list if super_event_list is not None else []

        ret = super_event_list + evnet_list

        return ret

    @classmethod
    def exec_event(cls, enevt_entiys, event_name):
        """
        执行某个事件
        :param enevt_entiys:
        :param event_name:
        :return:
        """
        event_generator = EventGenerator(event_name, enevt_entiys)
        event_gen = event_generator.render_json().get(event_name)

        sc = event_gen.get("script")
        print(">> ", sc)

        return sc